﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLPropertyGrid.TypeConverters
{
    public class CharConverter : TypeConverter
    {
        // Methods
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (!(value is string))
            {
                return base.ConvertFrom(context, culture, value);
            }
            string str = (string)value;
            if (str.Length > 1)
            {
                str = str.Trim();
            }
            if ((str == null) || (str.Length <= 0))
            {
                return '\0';
            }
            if (str.Length != 1)
            {
                throw new FormatException();
            }
            return str[0];
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (((destinationType == typeof(string)) && (value is char)) && (((char)value) == '\0'))
            {
                return "";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

}
